﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace HelloWindows8
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    public sealed partial class GameDienToanTu : HelloWindows8.Common.LayoutAwarePage
    {
        DispatcherTimer timer = new DispatcherTimer();

        int count = 0;
        int trueAnswerTotal = 0;
        int falseAnswerTotal = 0;
        int resultLeft = 0;
        int resultRight = 0;
        int trueAnswer = 0;
        public GameDienToanTu()
        {
            this.InitializeComponent();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            timer.Tick += timer_Tick;
            LoadData();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }



        public void LoadData()
        {
            count = 0;
            resultLeft = 0;
            resultRight = 0;
            operand1.Text = "+";
            operand3.Text = "+";
            resultTxt.Text = string.Format("Số lượng đán án đúng là: {0}", trueAnswerTotal);
            timer.Start();
        }

        void timer_Tick(object sender, object e)
        {
            if (count == 30)
            {
                timer.Stop();
                Caculater();
            }
            else
            {
                Random random = new Random();
                operator1.Text = random.Next(0, 10).ToString();
                operator2.Text = random.Next(0, 10).ToString();
                operator3.Text = random.Next(0, 10).ToString();
                operator4.Text = random.Next(0, 10).ToString();
                if (operand1.Text == "-")
                {
                    operand1.Text = "+";
                }
                else
                {
                    operand1.Text = "-";
                }
                if (operand3.Text == "-")
                {
                    operand3.Text = "+";
                }
                else
                {
                    operand3.Text = "-";
                }
                count++;
            }
        }

        public void Caculater()
        {
            Random random = new Random();
            int randomOperand = random.Next(0, 10);
            if ((randomOperand % 2) == 0)
            {
                operand1.Text = "+";
            }
            else
            {
                operand1.Text = "-";
            }
            randomOperand = random.Next(0, 10);
            if ((randomOperand % 2) == 0)
            {
                operand3.Text = "+";
            }
            else
            {
                operand3.Text = "-";
            }

            operator2.Text = random.Next(0, 10).ToString();
            if (operand1.Text == "-")
            {
                operator1.Text = random.Next(Int32.Parse(operator2.Text), 20).ToString();
                resultLeft = Int32.Parse(operator1.Text) - Int32.Parse(operator2.Text);
            }
            else
            {
                operator1.Text = random.Next(0, 20).ToString();
                resultLeft = Int32.Parse(operator1.Text) + Int32.Parse(operator2.Text);
            }

            operator4.Text = random.Next(0, 10).ToString();
            if (operand3.Text == "-")
            {
                operator3.Text = random.Next(Int32.Parse(operator4.Text), 20).ToString();
                resultRight = Int32.Parse(operator3.Text) - Int32.Parse(operator4.Text);
            }
            else
            {
                operator3.Text = random.Next(0, 20).ToString();
                resultRight = Int32.Parse(operator3.Text) + Int32.Parse(operator4.Text);
            }

        }


        private void answerButton1_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            int tempStr = Int32.Parse(btn.Tag.ToString());

            if (resultLeft > resultRight)
                trueAnswer = 1;
            if (resultLeft == resultRight)
                trueAnswer = 2;
            if (resultLeft < resultRight)
                trueAnswer = 3;



            if ((trueAnswerTotal + falseAnswerTotal) != 10)
            {
                if (tempStr == trueAnswer)
                {
                    trueAnswerTotal++;
                    LoadData();
                }
                else
                {
                    falseAnswerTotal++;
                    LoadData();
                }
            }
            else
            {

            }
        }
    }
}
